package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RbacManagerService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RbacManagerController
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/8/19 17:14
 * @Version 1.0
 */
@RequestMapping("/rbacManager")
@RestController
@CrossOrigin
public class RbacManagerController {

    @Autowired
    RbacManagerService rbacManagerService;

    @PostMapping("/addOne")
    public ResponseEntity addOne(@RequestBody RbacManagerDTO user){
        return rbacManagerService.addUser(user);
    }
    @GetMapping("/selectAll/{currentPage}/{pageSize}")
    public ResponseEntity findAll(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize) {
        return rbacManagerService.selectAll(currentPage,pageSize);
    }
    @RequestMapping("/editOne")
    public ResponseEntity editOne(@RequestBody User user){
        return rbacManagerService.editOne(user);
    }
    @GetMapping("/search/{currentPage}/{pageSize}")
    public ResponseEntity search(@PathVariable("currentPage")int currentPage,@PathVariable("pageSize") int pageSize,@RequestParam String account,@RequestParam String roleName){
        return rbacManagerService.search(currentPage,pageSize,account,roleName);
    }
    @RequestMapping("/deleteById")
    public ResponseEntity deleteById(String id){
        int rows = rbacManagerService.deleteUserById(Integer.parseInt(id));
        if(rows==0){
            return new ResponseEntity(404,"因为不明原因失败了",null);
        }else{
            return new ResponseEntity(200,"成功删除了",null);
        }
    }
    @GetMapping("/editType/{id}")
    public ResponseEntity editOne(@PathVariable int id){
        return rbacManagerService.editType(id);
    }
    @GetMapping("/initPwd/{id}")
    public ResponseEntity initPwd(@PathVariable int id){
        return rbacManagerService.initPwd(id);
    }
    @PostMapping("/updateRole")
    public ResponseEntity updateRole(@RequestBody RbacManagerDTO rbacManagerDTO){
        return rbacManagerService.updateRole(rbacManagerDTO);
    }
    public List<User> findUserByName(String username){
        List<User> list = rbacManagerService.findUserByName(username);
        return list;
    }
}
